package com.situ.skymall.user.api.controller;

import com.situ.model.UserAddress;
import com.situ.service.UserAddressService;
import com.situ.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户地址Controller
 */
@RestController
@RequestMapping("/api/v1/address")
public class UserAddressController {

    private final UserAddressService userAddressService;

    @Autowired
    public UserAddressController(UserAddressService userAddressService) {
        this.userAddressService = userAddressService;
    }

    /**
     * 获取用户的所有地址
     */
    @GetMapping("/list/{userId}")
    public JsonResult<?> findByUserId(@PathVariable Integer userId) {
        try {
            List<UserAddress> addresses = userAddressService.findByUserId(userId);
            return JsonResult.success(addresses);
        } catch (Exception e) {
            return JsonResult.fail("获取地址列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户的默认地址
     */
    @GetMapping("/default/{userId}")
    public JsonResult<?> findDefaultAddress(@PathVariable Integer userId) {
        try {
            UserAddress address = userAddressService.findDefaultAddress(userId);
            if (address == null) {
                // 未找到默认地址是正常情况，不应返回404错误，而是成功返回null
                return JsonResult.success(null);
            }
            return JsonResult.success(address);
        } catch (Exception e) {
            return JsonResult.fail("获取默认地址失败: " + e.getMessage());
        }
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/default")
    public JsonResult<?> setDefaultAddress(@RequestParam Integer userId, @RequestParam Integer addressId) {
        try {
            boolean result = userAddressService.setDefaultAddress(addressId, userId);
            if (result) {
                return JsonResult.success("设置成功");
            } else {
                return JsonResult.fail("设置默认地址失败");
            }
        } catch (Exception e) {
            return JsonResult.fail("设置默认地址失败: " + e.getMessage());
        }
    }

    /**
     * 新增用户地址
     */
    @PostMapping("/add")
    public JsonResult<?> addAddress(@RequestBody UserAddress userAddress) {
        try {
            boolean result = userAddressService.addAddress(userAddress);
            if (result) {
                return JsonResult.success("新增地址成功");
            } else {
                return JsonResult.fail("新增地址失败");
            }
        } catch (Exception e) {
            return JsonResult.fail("新增地址失败: " + e.getMessage());
        }
    }

    /**
     * 修改用户地址
     */
    @PutMapping("/update")
    public JsonResult<?> updateAddress(@RequestBody UserAddress userAddress) {
        try {
            boolean result = userAddressService.updateAddress(userAddress);
            if (result) {
                return JsonResult.success("修改地址成功");
            } else {
                return JsonResult.fail("修改地址失败");
            }
        } catch (Exception e) {
            return JsonResult.fail("修改地址失败: " + e.getMessage());
        }
    }

    /**
     * 删除用户地址
     */
    @DeleteMapping("/delete/{id}")
    public JsonResult<?> deleteAddress(@PathVariable Integer id) {
        try {
            boolean result = userAddressService.deleteAddress(id);
            if (result) {
                return JsonResult.success("删除成功");
            } else {
                return JsonResult.fail("删除地址失败");
            }
        } catch (Exception e) {
            return JsonResult.fail("删除地址失败: " + e.getMessage());
        }
    }
}

